<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .basic-drawer-footer {
    margin-left: 290px;
    margin-top: 50px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>基础设置</span>
    </div>
    <div class="line"></div>
    <Form
      ref="basicManageForm"
      :model="form"
      :rules="ruleInline"
      style="width: 100%; margin-left: 100px; margin-top: 50px;"
      :label-width="150">
      <FormItem prop="name" label="公司名称：" style="width: 400px">
        <Input  type="text" v-model="configListData['c_name']['value']" placeholder="请输入公司名称" style="margin-left: 40px;" />
      </FormItem>
      <FormItem prop="introduce" label="公司简介：" style="width: 600px">
        <Input  type="textarea" v-model="configListData['c_profile']['value']" placeholder="请输入公司简介" :rows="6" style="margin-left: 40px;" />
      </FormItem>
      <FormItem prop="img" label="公司图片：" style="width: 400px">
        <system-upload-img @upload-success="handleUploadImg" :folder="folder" :img="configListData['c_img']['value']" :hosturl="configListData['host']"  style="margin-left: 40px;"></system-upload-img>
      </FormItem>
      <FormItem prop="logo" label="公司LOGO：" style="width: 400px">
        <system-upload-logo @upload-success="handleUploadLogo" :folder="logoFolder" :logo="configListData['c_logo']['value']" :hosturl="configListData['host']" style="margin-left: 40px;"></system-upload-logo>
      </FormItem>
      <FormItem prop="tel" label="公司电话：" style="width: 400px">
        <Input  type="text" v-model="configListData['c_phone']['value']" placeholder="请输入公司电话" style="margin-left: 40px;" />
      </FormItem>
      <FormItem prop="address" label="公司地址：" style="width: 400px">
        <Input  type="text" v-model="configListData['c_address']['value']" placeholder="请输入公司地址" style="margin-left: 40px;" />
      </FormItem>
      <!-- <FormItem prop="code" label="企业二维码：" style="width: 400px">
        <upload-img @upload-success="handleUploadCode" :folder="folder" style="margin-left: 40px;"></upload-img>
      </FormItem> -->
      <FormItem prop="protocol" label="用户协议：" style="width: 400px">
        <Button @click="handleProtocol" style="margin-left: 40px;">协议配置</Button>
      </FormItem>
    </Form>
    <protocol-modal :modalState="modalState" v-if="modalState" @protocol-valid="handleEdit"></protocol-modal>
    <div class="basic-drawer-footer">
      <Button style="margin-right: 50px; width: 100px;" size="large" type="primary" @click="handleSubmit">保存</Button>
      <!-- <Button style="width: 100px;" @click="cancel" size="large">取消</Button> -->
    </div>
  </div>
</template>
<script>
import SystemUploadImg from '_c/system-upload-img'
import SystemUploadLogo from '_c/system-upload-logo'
import ProtocolModal from '_c/protocol-modal'
import { mapActions, mapState } from 'vuex'
export default {
  components: {
    ProtocolModal,
    SystemUploadImg,
    SystemUploadLogo
  },
  data () {
    return {
      title: '',
      imgUrl: '',
      codeUrl: '',
      logoUrl: '',
      folder: 'company',
      content: '',
      name: '',
      companyName: '',
      confirmData: [],
      logoFolder: 'logo',
      form: {
        name: '',
        introduce: '',
        tel: '',
        address: ''
      },
      postData: {},
      loading: true,
      ruleInline: {
        // name: [
        //   { required: true, message: '姓名不能为空', trigger: 'blur' }
        // ],
        // tel: [
        //   { required: true, message: '手机号不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  computed: {
    ...mapState({
      configListData: state => state.parameterConfig.configListData,
      modalState: state => state.modal.modalState
    })
  },
  methods: {
    ...mapActions([
      'updateModalState',
      'editBasicData',
      'getConfigListData'
    ]),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleEdit (playload) {
      // console.log(playload)
      this.updateModalState(false)
      this.content = playload.content
    },
    async handleSave (playload) {
      const resp = await this.editBasicData(playload)
      if (resp.data.state === '000') {
        this.$Message.success('保存成功')
        this.getConfigListData({})
      } else {
        this.$Message.error('保存失败')
      }
    },
    handleSubmit () {
      this.$refs.basicManageForm.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.configListData['c_img']['value'] = this.imgUrl ? this.imgUrl : this.configListData['c_img']['value']
        this.configListData['c_logo']['value'] = this.logoUrl ? this.logoUrl
          : this.configListData['c_logo']['value']
        this.configListData['c_serviceAgree']['value'] = this.content
        Object.keys(this.configListData).map(key => {
          this.postData[this.configListData[key].id] = this.configListData[key].value
        })
        // console.log(this.postData)
        // this.postData = {
        //   1: this.configListData.name,
        //   2: this.configListData.des,
        //   5: this.imgUrl ? this.imgUrl : this.configListData.img,
        //   15: this.logoUrl ? this.logoUrl : this.configListData.logo,
        //   21: this.configListData.tel,
        //   3: this.configListData.address,
        //   6: this.content ? this.content : this.configListData.protocol
        //   // code: this.codeUrl
        // }
        // console.log(this.postData)
        this.handleSave(this.postData)
        // const resp = await this.editBasicData(this.postData).then(
        //   setTimeout(() => {
        //     this.getConfigListData({})
        //   }, 100)
        // )
      })
    },
    cancel () {
      this.$refs['basicManageForm'].resetFields()
    },
    handleUploadImg (url) {
      // console.log(url)
      this.imgUrl = url
    },
    handleUploadCode (url) {
      this.codeUrl = url
    },
    handleUploadLogo (url) {
      this.logoUrl = url
    },
    handleProtocol () {
      this.updateModalState(true)
    }
  }
}
</script>
<style>

</style>
